package com.hiultra.archive.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.mapper.ArcArchiveMapper;
import com.hiultra.archive.mapper.ArchiveBoxinnerMoveTaskMapper;
import com.hiultra.archive.mapper.RoomArchiveMapper;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.ArchiveBoxinnerMoveMapper;
import com.hiultra.archive.service.IArchiveBoxinnerMoveService;

/**
 * 盒间移位详情Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveBoxinnerMoveServiceImpl implements IArchiveBoxinnerMoveService {
    @Autowired
    private ArchiveBoxinnerMoveMapper archiveBoxinnerMoveMapper;
    @Autowired
    private ArchiveBoxinnerMoveTaskMapper archiveBoxinnerMoveTaskMapper;
    @Autowired
    private ArcArchiveMapper arcArchiveMapper;
    @Autowired
    private RoomArchiveMapper roomArchiveMapper;

    /**
     * 查询盒间移位详情
     *
     * @param id 盒间移位详情主键
     * @return 盒间移位详情
     */
    @Override
    public ArchiveBoxinnerMove selectArchiveBoxinnerMoveById(String id) {
        return archiveBoxinnerMoveMapper.selectArchiveBoxinnerMoveById(id);
    }

    /**
     * 查询盒间移位详情列表
     *
     * @param archiveBoxinnerMove 盒间移位详情
     * @return 盒间移位详情
     */
    @Override
    public List<ArchiveBoxinnerMove> selectArchiveBoxinnerMoveList(ArchiveBoxinnerMove archiveBoxinnerMove) {
        return archiveBoxinnerMoveMapper.selectArchiveBoxinnerMoveList(archiveBoxinnerMove);
    }

    /**
     * 新增盒间移位详情
     *
     * @param archiveBoxinnerMove 盒间移位详情
     * @return 结果
     */
    @Override
    public int insertArchiveBoxinnerMove(ArchiveBoxinnerMove archiveBoxinnerMove) {
        return archiveBoxinnerMoveMapper.insertArchiveBoxinnerMove(archiveBoxinnerMove);
    }

    /**
     * 修改盒间移位详情
     *
     * @param archiveBoxinnerMove 盒间移位详情
     * @return 结果
     */
    @Override
    public int updateArchiveBoxinnerMove(ArchiveBoxinnerMove archiveBoxinnerMove) {
        return archiveBoxinnerMoveMapper.updateArchiveBoxinnerMove(archiveBoxinnerMove);
    }

    /**
     * 批量删除盒间移位详情
     *
     * @param ids 需要删除的盒间移位详情主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxinnerMoveByIds(String[] ids) {
        return archiveBoxinnerMoveMapper.deleteArchiveBoxinnerMoveByIds(ids);
    }

    /**
     * 删除盒间移位详情信息
     *
     * @param id 盒间移位详情主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxinnerMoveById(String id) {
        return archiveBoxinnerMoveMapper.deleteArchiveBoxinnerMoveById(id);
    }

    /**
     * 根据任务id查询盒移位信息详情
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ArchiveBoxinnerMove> selectArchiveBoxinnerMoveByTaskId(String taskId) {
        return archiveBoxinnerMoveMapper.selectArchiveBoxinnerMoveByTaskId(taskId);
    }

    @Override
    public AjaxResult updateArchiveBoxMoveList(List<ArchiveBoxinnerMove> archiveBoxinnerMoveList, String userName) {
        for (ArchiveBoxinnerMove archiveBoxinnerMove : archiveBoxinnerMoveList) {
            //记录操作人和操作时间   子表位置
            //todo 主表状态
            RoomArchive roomArchive = roomArchiveMapper.selectRoomArchiveByName(archiveBoxinnerMove.getArchName());
            if (ObjectUtil.isNotNull(roomArchive)) {
                roomArchive.setBoxNo(archiveBoxinnerMove.getNewBoxNo());
                roomArchive.setBagNo(archiveBoxinnerMove.getNewBagNo());
                roomArchive.setHouseLocationNo(archiveBoxinnerMove.getNewHouseLocationNo());

                roomArchiveMapper.updateRoomArchive(roomArchive);
                archiveBoxinnerMoveMapper.updateArchiveBoxinnerMove(archiveBoxinnerMove);
            } else {
                throw new ServiceException("未查询到档案信息，请检查重试！");
            }

        }
        return AjaxResult.success();
    }
}
